package com.ciwong.epaper.modules.pad.homeworkwidget.widget.remoteviews;

import android.content.Context;
import android.graphics.Bitmap;

import com.ciwong.epaper.modules.pad.homeworkwidget.presenter.HomeworkWidgetCheckState;


/**
 * date :2018/8/29
 * author: lzh
 * description:代理类
 * version:2.10
 */
public class HomeworkRemoteViewsDecorator {
//    Decorator

    private static HomeworkRemoteViewsDecorator instance;

    private HomeworkRemoteViewsDecorator() {
    }

    public static HomeworkRemoteViewsDecorator getInstance() {
        if (instance == null) {
            instance = new HomeworkRemoteViewsDecorator();
        }
        return instance;
    }

    /**
     * 刷新listview
     *
     * @param context
     */

    public void refreshListData(Context context) {
        HomeworkRemoteViews homeworkRemoteViews = new HomeworkRemoteViews(context);
        checkState(context, homeworkRemoteViews);
        homeworkRemoteViews.loadComplete();
        homeworkRemoteViews.notifyAppWidgetViewDataChanged();

    }

    /**
     * 刷新整个View
     *
     * @param context
     */

    public void refreshAllView(Context context) {
        HomeworkRemoteViews homeworkRemoteViews = new HomeworkRemoteViews(context);
        checkState(context, homeworkRemoteViews);
        homeworkRemoteViews.notifyAppWidgetViewDataChanged();

    }

    /**
     * 判断是否登录
     *
     * @param context
     */
    public void checkIsLoginAndJoinClassState(Context context) {
        if (context == null) return;
        boolean flagLogin = false;
        boolean flagJoinClass = true;
        boolean isBindPhone = false;

        if (HomeworkWidgetCheckState.isLogin(context)) {//登陆成功
            flagLogin = true;
            if (HomeworkWidgetCheckState.isBindPhone()) {
                isBindPhone = true;
            }
        }
        HomeworkRemoteViews homeworkRemoteViews = new HomeworkRemoteViews(context);
        homeworkRemoteViews.checkIsLogin(flagLogin, isBindPhone, flagJoinClass);
        homeworkRemoteViews.updateAppWidget();
    }

    /**
     * 判断是否登录，绑定，加班
     *
     * @param context
     * @param homeworkRemoteViews
     */
    public void checkState(Context context, HomeworkRemoteViews homeworkRemoteViews) {
        if (context == null) return;
        boolean flagLogin = false;
        boolean flagJoinClass = true;
        boolean isBindPhone = false;

        if (HomeworkWidgetCheckState.isLogin(context)) {//登陆成功
            flagLogin = true;
            if (HomeworkWidgetCheckState.isBindPhone()) {
                isBindPhone = true;
            }
        }
        homeworkRemoteViews.checkIsLogin(flagLogin, isBindPhone, flagJoinClass);
        homeworkRemoteViews.updateAppWidget();
    }

    /**
     * @param context
     * @param islogin
     * @param isBindPhone 是否登陆
     * @param isJoined    是否加入班级
     * @hide
     */
    public void checkIsLogin(Context context, boolean islogin, boolean isBindPhone, boolean isJoined) {
        HomeworkRemoteViews homeworkRemoteViews = new HomeworkRemoteViews(context);
        homeworkRemoteViews.checkIsLogin(islogin, isBindPhone, isJoined);
        homeworkRemoteViews.updateAppWidget();
    }

    public void setPersonalViewHeadBitmap(Context context, Bitmap bitmap) {
        HomeworkRemoteViews homeworkRemoteViews = new HomeworkRemoteViews(context);
        homeworkRemoteViews.setPersonalViewHeadBitmap(bitmap);
        checkState(context, homeworkRemoteViews);
        homeworkRemoteViews.updateAppWidget();
    }

    public void setPersonalViewHeadResource(Context context, int srcId) {
        HomeworkRemoteViews homeworkRemoteViews = new HomeworkRemoteViews(context);
        homeworkRemoteViews.setPersonalViewHeadResource(srcId);
        checkState(context, homeworkRemoteViews);
        homeworkRemoteViews.updateAppWidget();
    }

    public void setPersonalViewSchool(Context context, String schoolInfor) {
        HomeworkRemoteViews homeworkRemoteViews = new HomeworkRemoteViews(context);
        homeworkRemoteViews.setPersonalViewSchool(schoolInfor);
        checkState(context, homeworkRemoteViews);
        homeworkRemoteViews.updateAppWidget();
    }

    public void setPersonalViewVip(Context context, String strVip) {
        HomeworkRemoteViews homeworkRemoteViews = new HomeworkRemoteViews(context);
        homeworkRemoteViews.setPersonalViewVip(strVip);
        checkState(context, homeworkRemoteViews);
        homeworkRemoteViews.updateAppWidget();
    }

    public void setPersonalViewBaseIfor(Context context, String strBaseInfor) {
        HomeworkRemoteViews homeworkRemoteViews = new HomeworkRemoteViews(context);
        homeworkRemoteViews.setPersonalViewBaseIfor(strBaseInfor);
        checkState(context, homeworkRemoteViews);
        homeworkRemoteViews.updateAppWidget();
    }

    public void loading(Context context) {
        HomeworkRemoteViews homeworkRemoteViews = new HomeworkRemoteViews(context);
        homeworkRemoteViews.loading();
    }

}
